package com.talos.ums.controller.admin.organization;

import com.talos.framework.apilog.core.annotation.ApiAccessLog;
import com.talos.framework.common.pojo.CommonResult;
import com.talos.framework.common.pojo.PageParam;
import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.framework.excel.core.util.ExcelUtils;
import com.talos.ums.controller.admin.organization.vo.UmsOrganizationPageReqVO;
import com.talos.ums.controller.admin.organization.vo.UmsOrganizationRespVO;
import com.talos.ums.controller.admin.organization.vo.UmsOrganizationSaveReqVO;
import com.talos.ums.dal.dataobject.organization.UmsOrganizationDO;
import com.talos.ums.service.organization.UmsOrganizationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.talos.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.talos.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 组织表")
@RestController
@RequestMapping("/ums/organization")
@Validated
public class UmsOrganizationController {

    @Resource
    private UmsOrganizationService organizationService;

    @PostMapping("/create")
    @Operation(summary = "创建组织表")
    @PreAuthorize("@ss.hasPermission('ums:organization:create')")
    public CommonResult<Long> createOrganization(@Valid @RequestBody UmsOrganizationSaveReqVO createReqVO) {
        return success(organizationService.createOrganization(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组织表")
    @PreAuthorize("@ss.hasPermission('ums:organization:update')")
    public CommonResult<Boolean> updateOrganization(@Valid @RequestBody UmsOrganizationSaveReqVO updateReqVO) {
        organizationService.updateOrganization(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除组织表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ums:organization:delete')")
    public CommonResult<Boolean> deleteOrganization(@RequestParam("id") Long id) {
        organizationService.deleteOrganization(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组织表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ums:organization:query')")
    public CommonResult<UmsOrganizationRespVO> getOrganization(@RequestParam("id") Long id) {
        UmsOrganizationDO organization = organizationService.getOrganization(id);
        return success(BeanUtils.toBean(organization, UmsOrganizationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得组织表分页")
    @PreAuthorize("@ss.hasPermission('ums:organization:query')")
    public CommonResult<PageResult<UmsOrganizationRespVO>> getOrganizationPage(@Valid UmsOrganizationPageReqVO pageReqVO) {
        PageResult<UmsOrganizationDO> pageResult = organizationService.getOrganizationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UmsOrganizationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出组织表 Excel")
    @PreAuthorize("@ss.hasPermission('ums:organization:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrganizationExcel(@Valid UmsOrganizationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UmsOrganizationDO> list = organizationService.getOrganizationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "组织表.xls", "数据", UmsOrganizationRespVO.class,
                        BeanUtils.toBean(list, UmsOrganizationRespVO.class));
    }

}
